#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import traceback
import logging
import logging.config

logger = logging.getLogger("debug")

def init_log(log_path, log_name, log_level = "DEBUG"):
    log_level = log_level.upper()
    LOG_PATH_DEBUG = "%s/%s.log" % (log_path, log_name)
    LOG_FILE_MAX_BYTES = 100 * 1024 * 1024
    LOG_FILE_BACKUP_COUNT = 30

    log_conf = {
        "version" : 1,
        "formatters" : {
            "format1" : {
                "format" : '%(asctime)-15s [%(thread)d] - [%(filename)s %(lineno)d] %(levelname)s %(message)s',
            },
        },

        "handlers" : {
            "handler": {
                "class" : "logging.handlers.TimedRotatingFileHandler",
                "level" : log_level,
                "formatter" : "format1",
                "when" : "midnight",
                "backupCount" : LOG_FILE_BACKUP_COUNT,
                "filename" : LOG_PATH_DEBUG
            },
        },

        "loggers" : {
            "debug": {
                "handlers" : ["handler"],
                "level" : log_level
            },
        }
    }
    logging.config.dictConfig(log_conf)

def close_log():
    logging.shutdown()
